package org.yinweichen.llm_dataset_backend.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.yinweichen.llm_dataset_backend.DTO.OriginalAnswerCreateDTO;
import org.yinweichen.llm_dataset_backend.entity.OriginalAnswer;
import org.yinweichen.llm_dataset_backend.entity.OriginalQuestion;
import org.yinweichen.llm_dataset_backend.repository.OriginalAnswerRepository;
import org.yinweichen.llm_dataset_backend.repository.OriginalQuestionRepository;
import org.yinweichen.llm_dataset_backend.service.OriginalAnswerService;
import org.yinweichen.llm_dataset_backend.util.IdGenerator;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class OriginalAnswerServiceImpl implements OriginalAnswerService {
    private final OriginalAnswerRepository answerRepository;
    private final OriginalQuestionRepository questionRepository;
    private final IdGenerator idGenerator;

    @Override
    public OriginalAnswer create(OriginalAnswerCreateDTO request) {
        OriginalQuestion question = questionRepository
            .findByIdAndVersion(request.originalQuestionId(), request.version())
            .orElseThrow(() -> new IllegalArgumentException("OriginalQuestion not found"));

        OriginalAnswer answer = OriginalAnswer.builder()
            .id(idGenerator.generateUniqueIdForOriginalAnswer())
            .version(request.version())
            .answer(request.answer())
            .updateTime(new Date())
            .originalQuestion(question)
            .build();

        return answerRepository.save(answer);
    }
}
